package com.yuandian.ops.workbench.controller;

import com.yuandian.api.business_monitor.feign.RemoteBusinessDetailService;
import com.yuandian.api.business_monitor.po.QueryBusinessDetailParams;
import com.yuandian.api.business_monitor.po.QueryBusinessStepParams;
import com.yuandian.api.business_monitor.po.QueryEndToEndDetailParams;
import com.yuandian.api.business_monitor.vo.BusinessDetailVo;
import com.yuandian.api.business_monitor.vo.EndToEndRelationVo;
import com.yuandian.api.business_monitor.vo.EndToEndShowVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.utils.PageDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明：业务详情Controller
 * @Author yanjun
 * @Date 2020/9/7 10:00
 */
@RestController
@RequestMapping("/business/detail")
@Api(tags = "用户详情接口")
@Slf4j
public class BusinessDetailController {

    @Autowired
    private RemoteBusinessDetailService businessDetailService;


    /**
     * 步骤端到端实时关联
     * @param endToEndRelationVo 端到端关联vo对象
     * @return 通用返回对象
     */
	@ApiOperation(value = "业务步骤IT路径图", notes = "业务步骤IT路径图")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  				 paramType = "query", required = true,   dataType = "Integer"),
			@ApiImplicitParam(name = "preRelvanceId",  		value = "首端数据id",  			 paramType = "query", required = true,   dataType = "List<Integer>"),
			@ApiImplicitParam(name = "resourceCode",  		value = "资源编码",  			 paramType = "query", required = true,   dataType = "Integer"),
			@ApiImplicitParam(name = "type", 				value = "类型 1：加载项 2：步骤",   paramType = "query", required = true,   dataType = "List<String>"),
			@ApiImplicitParam(name = "startTimeUsec", 		value = "开始时间戳", 			 paramType = "query", required = true,   dataType = "Long"),
			@ApiImplicitParam(name = "endTimeUsec",   		value = "结束时间戳", 			 paramType = "query", required = true,   dataType = "Long")
	})
	@PostMapping("/stepEndToEndRelation")
    public R<List<EndToEndShowVo>> endToEndRelation(@RequestBody EndToEndRelationVo endToEndRelationVo) {
        List<EndToEndShowVo> list = new ArrayList<>();

		try {
			long sTime = System.currentTimeMillis();
			list = businessDetailService.endToEndRelation(endToEndRelationVo).getData();
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[步骤端到端实时关联]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[步骤端到端实时关联]报错: {}" + e.getMessage());
		}

        return R.ok(list);
    }


    /**
     * 查询端到端节点详情
     * @param params 查询端到端节点详情数据参数
     * @return 通用返回对象
     */
	@ApiOperation(value = "节点信息", notes = "节点信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "codeList",  		value = "查询参数字符串",  		 	paramType = "query", required = false,  dataType = "List<String>"),
			@ApiImplicitParam(name = "dataId",  		value = "数据id",  			 		paramType = "query", required = true,   dataType = "String"),
			@ApiImplicitParam(name = "dataType",  		value = "数据类型:step web api sql", paramType = "query", required = true,   dataType = "String"),
			@ApiImplicitParam(name = "pageNum",   		value = "页码", 						paramType = "query", required = true,   dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 		value = "每页大小", 					paramType = "query", required = true,   dataType = "Long"),
			@ApiImplicitParam(name = "startTimeUsec", 	value = "开始时间戳", 			 	paramType = "query", required = true,   dataType = "Long"),
			@ApiImplicitParam(name = "endTimeUsec",   	value = "结束时间戳", 			 	paramType = "query", required = true,   dataType = "Long")
	})
	@PostMapping("/queryEndToEndDetail")
    public R<PageDataInfo> queryEndToEndDetail(@RequestBody QueryEndToEndDetailParams params) {
        PageDataInfo pageDataInfo = null;

		try {
			long sTime = System.currentTimeMillis();
			pageDataInfo = businessDetailService.queryEndToEndDetail(params).getData();
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[查询端到端节点详情]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[查询端到端节点详情]报错: {}" + e.getMessage());
		}

        return R.ok(pageDataInfo);
    }


	/**
	 * 根据详单id查询业务详情
	 * @param params 业务详情查询参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "业务详情", notes = "业务详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessDetailId",	value = "详单ID",  			 		paramType = "query", required = true,   dataType = "String"),
			@ApiImplicitParam(name = "startTimeUsec", 		value = "开始时间戳", 			 	paramType = "query", required = true,   dataType = "String"),
			@ApiImplicitParam(name = "endTimeUsec",   		value = "结束时间戳", 			 	paramType = "query", required = true,   dataType = "String")
	})
	@PostMapping("/queryBusinessDetailByDetailId")
	public R<BusinessDetailVo> queryBusinessDetailByDetailId(@RequestBody QueryBusinessDetailParams params) {
		BusinessDetailVo vo = new BusinessDetailVo();

		try {
			long sTime = System.currentTimeMillis();
			vo =  businessDetailService.queryBusinessDetailByDetailId(params).getData();
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[根据详单id查询业务详情]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[根据详单id查询业务详情]报错: {}" + e.getMessage());
		}

		return R.ok(vo);
	}


	/**
	 * 分页查询业务步骤列表
	 * @param params 查询业务步骤参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "业务步骤", notes = "业务步骤")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessDetailId",	value = "详单ID",  		paramType = "query",  required = true,   dataType = "String"),
			@ApiImplicitParam(name = "pageNum",   		value = "页码", 						paramType = "query", required = true,   dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 		value = "每页大小", 					paramType = "query", required = true,   dataType = "Long"),
			@ApiImplicitParam(name = "startTimeUsec", 		value = "开始时间戳", 	paramType = "query",  required = true,   dataType = "String"),
			@ApiImplicitParam(name = "endTimeUsec",   		value = "结束时间戳", 	paramType = "query",  required = true,   dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 		paramType = "query",  required = true,   dataType = "String"),
			@ApiImplicitParam(name = "endTime",   			value = "结束时间", 		paramType = "query",  required = true,   dataType = "String")
	})
	@PostMapping("/queryBusinessStepListPage")
	public R<PageDataInfo> queryBusinessStepListPage(@RequestBody QueryBusinessStepParams params) {
		PageDataInfo pageDataInfo = null;

		try {
			long sTime = System.currentTimeMillis();
			pageDataInfo =  businessDetailService.queryBusinessStepListPage(params).getData();
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[分页查询业务步骤列表]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[分页查询业务步骤列表]报错: {}" + e.getMessage());
		}

		return R.ok(pageDataInfo);
	}

}
